﻿/******************************************************************************
 * 【本类功能概述】                                 					      *
 *  版权所有（C）2017- 匠梦科技                                         *
 *  保留所有权利。                                                            *
 ******************************************************************************
 *  作者 : <hughjk>
 *  创建时间:2021年1月6日 14:06:11
 *  文件描述:书签
 *****************************************************************************/
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
using Util;
using System;

namespace UtilEditors
{
    [Serializable]
    public class Bookmark
    {
        public string Name;
        public string Path;
        public bool Toggle;
        public bool isAsset;
        public Object _Obj;
        public void Init(Object _obj)
        {
            _Obj = _obj;
            Name = _obj.name;
            bool Contains = AssetDatabase.Contains(_obj);
            if(Contains)
            {
                Path = AssetDatabase.GetAssetPath(_obj);
                isAsset = Path.isTrue();
            }
            else
            {
                var assobj = PrefabUtility.GetCorrespondingObjectFromSource(_obj);
                if(assobj)
                {
                    Path = AssetDatabase.GetAssetPath(assobj);
                    isAsset = Path.isTrue();
                }
                else
                {
                    isAsset = false;
                }
            }
        }
        public Object GetObj()
        {
            if(isAsset)
            {
                if(_Obj)
                {
                    return _Obj;
                }
                _Obj = AssetDatabase.LoadMainAssetAtPath(Path);
                return _Obj;
            }
            else
            {
                return _Obj;
            }
        }
    }
    public class SaveBookmarkNode
    {
        public string Name;
        public string Path;
    }



    public class BookmarkMgr : GUILayoutCom
    {
        private static BookmarkMgr ins;
        public static BookmarkMgr Ins
        {
            get
            {
                if(ins == null)
                {
                    ins = new BookmarkMgr();
                }
                return ins;
            }
        }
        const string pathss = "Assets/Plugins/Library/myTool/Editor/Window/Bookmark";
        string paths = pathss + "/BookmarkCache.asset";
        string fkey = "SaveBookmark";
        private BookmarkCache so;
        public BookmarkCache So
        {
            get
            {
                if(so.isNull())
                {
                    var list = EditorSave.GetConfigValue<List<SaveBookmarkNode>>(fkey);

                    so = (BookmarkCache) AssetDatabase.LoadMainAssetAtPath(paths);
                    if(!so)
                    {
                        so = new BookmarkCache();
                        FileIO.CreateSavePath(pathss);
                        AssetDatabase.CreateAsset(so , paths);
                        so = (BookmarkCache) AssetDatabase.LoadMainAssetAtPath(paths);
                    }
                    if(list.isTrue())
                    {
                        so.list = new List<Bookmark>();
                        for(int i = 0 ; i < list.Count ; i++)
                        {
                            so.list.Add(new Bookmark()
                            {
                                Name = list[i].Name ,
                                Path = list[i].Path ,
                                isAsset = true ,
                                Toggle = false ,
                                _Obj = AssetDatabase.LoadMainAssetAtPath(list[i].Path) ,
                            });
                        }
                    }
                    else
                    {
                        Save();
                    }
                }
                return so;
            }
        }
        public void Save()
        {
            List<SaveBookmarkNode> list = new List<SaveBookmarkNode>();
            for(int i = 0 ; i < so.list.Count ; i++)
            {
                if(so.list[i].isAsset)
                {
                    list.Add(new SaveBookmarkNode()
                    {
                        Name = so.list[i].Name ,
                        Path = so.list[i].Path
                    });
                }
            }
            EditorSave.SetConfigValue(fkey , list);
        }
        public override void OnGUI()
        {
            ComY(() =>
            {
                Text("书签");
                if(Button("纪录"))
                {
                    var objs = Selection.objects;
                    for(int i = 0 ; i < objs.Length ; i++)
                    {
                        var bk = new Bookmark();
                        bk.Init(objs[i]);
                        So.list.Add(bk);
                    }
                    Save();
                }
                for(int i = So.list.Count - 1 ; i >= 0 ; i--)
                {
                    var cur = So.list[i];
                    ComX(() =>
                    {
                        if(Button(cur.Name , GUILayout.MaxWidth(80)))
                        {
                            Object obj = null;
                            obj = cur.GetObj();
                            if(obj)
                            {
                                if(!Process(cur))
                                {
                                    Selection.activeObject = obj;
                                }
                            }
                            else
                            {
                                So.list.Remove(cur);
                                Save();
                            }
                        }
                        ChangeCheck(() =>
                        {
                            Toggle("" , ref cur.Toggle);
                        } , Save);
                    });
                    if(cur.Toggle)
                    {
                        ComX(() =>
                        {
                            Inpt("" , ref cur.Name);
                            if(Button("x" , GUILayout.MaxWidth(20)))
                            {
                                So.list.Remove(cur);
                                Save();
                            }
                        });
                    }
                }
            } , GUILayout.Width(100));
        }
        public bool Process(Bookmark book)
        {
            //Type Seletype = Selection.activeObject.GetType();
            Type objType = book._Obj.GetType();
            debug.log(objType);

            if(book._Obj is Material)
            {
                var arr = Selection.transforms;
                bool isok = false;
                var mat = book._Obj as Material;
                for(int i = 0 ; i < arr.Length ; i++)
                {
                    var tf = arr[i];
                    var sr = tf.GetComponent<SpriteRenderer>();
                    if(sr)
                    {
                        Undo.RecordObject(sr , "替换材质球");
                        sr.material = mat;
                        isok = true;
                    }
                }
                if(isok)
                {
                    return true;
                }
            }
            return false;
        }
    }


}
